package com.hezhou.oauth2.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hezhou.oauth2.web.entities.SysRole;
import com.hezhou.oauth2.web.entities.SysUser;
import com.hezhou.oauth2.web.mapper.SysRoleMapper;
import com.hezhou.oauth2.web.mapper.SysUserMapper;
import com.hezhou.oauth2.web.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ---------------------------
 * (SysUserServiceImpl) 系统用户逻辑实现层
 * ---------------------------
 *
 * @Author: [hezhou]
 * @Date: 2020/3/6
 * @Version: [1.0.1]
 * ---------------------------
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    public static final String PASS_WORD = "$10$rDkPvvAFV8kqwvKJzwlRv.i.q.wz1w1pz0SFsHn/55jNeZFQv/eCm";

    /**
     * 根据名称查询他的用户信息
     * @param name 用户名
     * @return
     */
    @Override
    public SysUser findByUserName(String name) {
        //防止报空指针
        if (StringUtils.isEmpty(name)){
           return null;
        }
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",name);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        return sysUser;
    }

    /**
     * 用电话查询用户的权限信息
     * @param mobile
     * @return
     */
    @Override
    public SysUser findByUserMobile(String mobile) {
        if (StringUtils.isEmpty(mobile)){
            return null;
        }
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        return sysUser;
    }

    /**
     * 分页模糊查询用户信息
     * @param page
     * @param sysUser
     * @return
     */
    @Override
    public IPage<SysUser> selectPageByCondition(Page<SysUser> page, SysUser sysUser) {
        IPage<SysUser> pageByCondition = sysUserMapper.findPageByCondition(page, sysUser);
        return pageByCondition;
    }

    /**
     * 根据用户id查询他的用户
     * @param userId
     * @return
     */
    @Override
    public SysUser findSysUserById(Long userId) {
        //为了防止报空指针
        if (userId == null){
            return new SysUser();
        }
        SysUser sysUser = sysUserMapper.selectById(userId);
        List<SysRole> sysRoles = sysRoleMapper.selectRoleByUserId(userId);
        if (sysRoles!=null){
            sysUser.setRoleList(sysRoles);
        }
        return sysUser;
    }

    /**
     * 修改用户信息
     * @param sysUser
     * @return
     */
    @Transactional
    @Override
    public Boolean saveOrUpdateUser(SysUser sysUser) {
        if (sysUser !=null && StringUtils.isBlank(sysUser.getPassword())){
            //设置密码
            sysUser.setPassword(PASS_WORD);
        }
        //先修改用户表的信息
        try{
            boolean b = super.saveOrUpdate(sysUser);
            if (b){
                //操作用户角色表
                baseMapper.deleteUserRoleByUserId(sysUser.getId());
                baseMapper.batchInsertUserRole(sysUser.getId(),sysUser.getRoleIds());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

}
